<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="js/react.js"></script>
    <script src="js/react-dom.js"></script>
    <script src="js/browser.min.js"></script>
    <script></script>
</head>
<body>
    <div id='example'></div>

    <script type="text/babel">
        var ToDoItem = React.createClass({
            handleClick:function(){
                //console.log(this.props.key);
                this.props
                .myDelete(this.props.myIndex);
            },
            render:function(){
                return <li>
                 <span>{this.props.myValue}</span>
                 <button onClick={this.handleClick}>delete</button>
                </li>
            }
        });
        var ToDoList = React.createClass({
            render:function(){
                return <ul>
                 {
                     this.props.myList
                     .map((value,index)=>{
                         return <ToDoItem 
                         myValue={value}
                         myIndex={index}
                myDelete={this.props.funcDelete}
                         key={index}>
                         </ToDoItem>
                     })
                 }
                </ul>
            }
        });
        var ToDoInput = React.createClass({
            handleClick:function(){
                //获取输入框的值
                var myValue = this.refs.myInput.value
                //发送给ToDoBox
                this.props.funcSave(myValue);
            },
            render:function(){
                return <div>
                  <h2>待做事项列表</h2>
                  <input type='text' ref="myInput"/>
                  <button onClick={this.handleClick}>add</button>
                </div>
            }
        });
        var ToDoBox = React.createClass({
            getInitialState:function(){
                return {
                    list:[]
                }
            },
            delete:function(index){
                var nowList = this.state.list;
                nowList.splice(index,1);
                this.setState({list:nowList});
            },
            save:function(msg){
                console.log('在ToDoBox中接收到ToDoInput所传来的数据是 '+msg);
                
                var nowList = this.state.list;
                nowList.push(msg);
                this.setState({list:nowList});

            },
            render:function(){
                return <div>
                 <ToDoInput 
                 funcSave={this.save}>
                 </ToDoInput>
                 <ToDoList 
                 funcDelete={this.delete}
                 myList={this.state.list}></ToDoList>
                </div>
            }
        });

        ReactDOM.render(
            <ToDoBox></ToDoBox>,
            document.getElementById('example')
        );
    </script>
</body>
</html>